// 评论业务服务
import {
  getComments as apiGetComments,
  publishComment as apiPublishComment,
  likeComment as apiLikeComment,
  dislikeComment as apiDislikeComment,
  markComment as apiMarkComment,
  updateComment as apiUpdateComment,
} from '@/api/modules/comment';
import { useAppStore } from '@/stores';
import { generateUUID } from '@/utils/common';
import type {
  GetCommentsParams,
  PublishCommentParams,
  LikeCommentParams,
  DislikeCommentParams,
  MarkCommentParams,
  UpdateCommentParams,
} from '@/types/comment';

/**
 * 评论服务类
 */
export class CommentService {
  /**
   * 获取评论列表
   */
  static async getComments(params: GetCommentsParams) {
    const xLogId = generateUUID();
    const appStore = useAppStore();

    try {
      const response = await apiGetComments(params, xLogId);

      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.msg || '获取评论列表失败');
      }
    } catch (error: any) {
      console.error('获取评论列表失败:', error);
      appStore.showToast(error.message || '获取评论列表失败', 'error');
      throw error;
    }
  }

  /**
   * 发布评论
   */
  static async publishComment(params: PublishCommentParams) {
    const xLogId = generateUUID();
    const appStore = useAppStore();

    try {
      const response = await apiPublishComment(params, xLogId);

      if (response.code === 200) {
        appStore.showToast('评论发布成功', 'success');
        return true;
      } else {
        throw new Error(response.msg || '评论发布失败');
      }
    } catch (error: any) {
      console.error('发布评论失败:', error);
      appStore.showToast(error.message || '评论发布失败', 'error');
      throw error;
    }
  }

  /**
   * 点赞/取消点赞评论
   */
  static async likeComment(commentId: number, userId: number, action: 'like' | 'unlike') {
    const xLogId = generateUUID();
    const appStore = useAppStore();

    try {
      const params: LikeCommentParams = { comment_id: commentId, user_id: userId, action };
      const response = await apiLikeComment(params, xLogId);

      if (response.code === 200) {
        const message = action === 'like' ? '点赞成功' : '取消点赞成功';
        appStore.showToast(message, 'success');
        return true;
      } else {
        throw new Error(response.msg || '操作失败');
      }
    } catch (error: any) {
      console.error('点赞评论失败:', error);
      appStore.showToast(error.message || '操作失败', 'error');
      throw error;
    }
  }

  /**
   * 点踩/取消点踩评论
   */
  static async dislikeComment(commentId: number, userId: number, action: 'dislike' | 'undislike') {
    const xLogId = generateUUID();
    const appStore = useAppStore();

    try {
      const params: DislikeCommentParams = { comment_id: commentId, user_id: userId, action };
      const response = await apiDislikeComment(params, xLogId);

      if (response.code === 200) {
        const message = action === 'dislike' ? '点踩成功' : '取消点踩成功';
        appStore.showToast(message, 'success');
        return true;
      } else {
        throw new Error(response.msg || '操作失败');
      }
    } catch (error: any) {
      console.error('点踩评论失败:', error);
      appStore.showToast(error.message || '操作失败', 'error');
      throw error;
    }
  }

  /**
   * 收藏/取消收藏评论
   */
  static async markComment(commentId: number, userId: number, action: 'mark' | 'unmark') {
    const xLogId = generateUUID();
    const appStore = useAppStore();

    try {
      const params: MarkCommentParams = { comment_id: commentId, user_id: userId, action };
      const response = await apiMarkComment(params, xLogId);

      if (response.code === 200) {
        const message = action === 'mark' ? '收藏成功' : '取消收藏成功';
        appStore.showToast(message, 'success');
        return true;
      } else {
        throw new Error(response.msg || '操作失败');
      }
    } catch (error: any) {
      console.error('收藏评论失败:', error);
      appStore.showToast(error.message || '操作失败', 'error');
      throw error;
    }
  }

  /**
   * 修改评论
   */
  static async updateComment(commentId: number, commentText: string) {
    const xLogId = generateUUID();
    const appStore = useAppStore();

    try {
      const params: UpdateCommentParams = { id: commentId, comment_text: commentText };
      const response = await apiUpdateComment(params, xLogId);

      if (response.code === 200) {
        appStore.showToast('评论修改成功', 'success');
        return true;
      } else {
        throw new Error(response.msg || '评论修改失败');
      }
    } catch (error: any) {
      console.error('修改评论失败:', error);
      appStore.showToast(error.message || '评论修改失败', 'error');
      throw error;
    }
  }
}
